﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ShaderPad
{
    /// <summary>
    /// Interaction logic for TVComboControl.xaml
    /// </summary>
    public partial class TVComboControl : UserControl 
    {
        // Provide CLR accessors for the event
        public event RoutedPropertyChangedEventHandler<LogicalTreeItem> ElementSelectionChanged;

        // This method raises the Tap event
        void RaiseElementSelectionChangedEvent(RoutedPropertyChangedEventArgs<LogicalTreeItem> e)
        {
            if (ElementSelectionChanged!=null)
                ElementSelectionChanged(this, e);
        }


        public TVComboControl()
        {
            InitializeComponent();
        }



        public FrameworkElement SelectedItem
        {
            get { return (FrameworkElement)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedItem.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem", typeof(FrameworkElement), typeof(TVComboControl), new UIPropertyMetadata(null));



        public DependencyObject ElementSource
        {
            get { return (DependencyObject)GetValue(ElementSourceProperty); }
            set
            {
                SetValue(ElementSourceProperty, value);
                SetRootElement(value);
            }
        }

        // Using a DependencyProperty as the backing store for ElementSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ElementSourceProperty =
            DependencyProperty.Register("ElementSource", typeof(DependencyObject), typeof(TVComboControl), new UIPropertyMetadata(null));



        private void SetRootElement(DependencyObject root)
        {
            LogicalTreeItem vti = new LogicalTreeItem(root);

            Part_TreeView.ItemsSource = vti.Children;
            if (vti.Children.Count() > 0)
            {
                vti.Children.First().IsSelected = true;
            }
        }


        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Part_ToggleButton.IsChecked = !Part_ToggleButton.IsChecked;
        }

        private void Part_ToggleButton_Checked(object sender, RoutedEventArgs e)
        {
            Part_ComboTextBox.Visibility = Visibility.Visible;
            Part_ComboTextBox.Focus();
        }

        private void Grid_LostFocus(object sender, RoutedEventArgs e)
        {
            Part_ToggleButton.IsChecked = false;
        }

        private void Part_ToggleButton_Unchecked(object sender, RoutedEventArgs e)
        {
            Part_ComboTextBox.Visibility = Visibility.Collapsed;
        }

        private void Part_TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            LogicalTreeItem lti = e.NewValue as LogicalTreeItem;

            if (lti != null)
            {
                this.SelectedItem = lti.Element as FrameworkElement;
                Part_ComboTextBox.Text = lti.Name;
                Part_Dropdown.IsOpen = false;
                RoutedPropertyChangedEventArgs<LogicalTreeItem> ee = new RoutedPropertyChangedEventArgs<LogicalTreeItem>(e.OldValue as LogicalTreeItem, lti, e.RoutedEvent);
                RaiseElementSelectionChangedEvent(ee);
            }
            else
            {
                this.SelectedItem = null;
            }
        }
    }
}
